package com.moxiao.yfjh.module.activity.task;

import com.alibaba.fastjson.JSONObject;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityInfo.vo.ActivityInfoUpdateReqVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityEnroll.ActivityEnrollDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.moxiao.yfjh.module.activity.service.activityEnroll.ActivityEnrollService;
import com.moxiao.yfjh.module.activity.service.activityInfo.ActivityInfoService;
import com.moxiao.yfjh.module.activity.task.domain.DelayTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/30 15:50
 **/
@Component
public class RedisDealyTask implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(RedisDealyTask.class);

    @Resource
    private RedisTemplate redisTemplate;

    private String delayQueueName = "delayQueue";

    @Resource
    private ActivityInfoService activityInfoService;

    @Resource
    private ActivityEnrollService activityEnrollService;


    @Override
    public void run(String... args) throws Exception {
        Executors.newSingleThreadExecutor().submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 获取一个到点的消息
                    Set<String> set = redisTemplate.opsForZSet().rangeByScore(delayQueueName, 0, System.currentTimeMillis(), 0, 1);
                    // 如果没有，就等等
                    if (set.isEmpty()) {
                        try {
                            Thread.sleep(5 * 1000);
                        } catch (InterruptedException e) {
                        }
                        // 继续执行
                        continue;
                    }
                    // 获取具体消息的key
                    String it = set.iterator().next();
                    // 删除成功
                    if (redisTemplate.opsForZSet().remove(delayQueueName, it) > 0) {
                        log.info("拿到要处理的结果 RedisDealyTask ---------------------------------------------------");
                        DelayTask delayTask = JSONObject.parseObject(it, DelayTask.class);
                        Object msg = delayTask.getMsg();
                        //活动id
                        //活动根据报名截止时间去变换状态，未到报名截止时间的一直是报名中，即使人员报满了，再看还是报名中
                        // 只不过对应的报名按钮变为了男/女方人数已满，可报名人数为0了。
                        // 当报名时间截止了之后状态变为未开始。活动只要没有经过核销，不管什么状态都可以申请退款。
                        String activityId = msg.toString();

                        ActivityInfoDO info = activityInfoService.getInfo(Long.valueOf(activityId));
                        if (Objects.nonNull(info)){
                            String menNum = info.getMenNum();
                            String womenNum = info.getWomenNum();
                            ActivityEnrollExportReqVO exportReqVO = new ActivityEnrollExportReqVO();
                            exportReqVO.setNo(info.getNo());
                            exportReqVO.setSex((byte) 1);
                            List<ActivityEnrollDO> enrollList = activityEnrollService.getEnrollList(exportReqVO);

                            ActivityEnrollExportReqVO exportReqVO1 = new ActivityEnrollExportReqVO();
                            exportReqVO1.setNo(info.getNo());
                            exportReqVO1.setSex((byte) 2);
                            List<ActivityEnrollDO> enrollList1 = activityEnrollService.getEnrollList(exportReqVO);
                            if(enrollList.size() == Integer.parseInt(menNum)  && enrollList1.size() == Integer.parseInt(womenNum)){
                                ActivityInfoUpdateReqVO updateReqVO = new ActivityInfoUpdateReqVO();
                                updateReqVO.setId(info.getId());
                                updateReqVO.setStatus((byte) 4);
                                activityInfoService.updateInfo(updateReqVO);
                            }else {
                                ActivityInfoUpdateReqVO updateReqVO = new ActivityInfoUpdateReqVO();
                                updateReqVO.setId(info.getId());
                                updateReqVO.setStatus((byte) 3);
                                activityInfoService.updateInfo(updateReqVO);
                            }
                        }
                    }
                }
            }
        });
    }



    private Date getDateBefore(){
        Calendar calendar = Calendar.getInstance();
        int min=calendar.get(Calendar.MINUTE);
        //获取20分钟前的时间
        min -=20;
        calendar.set(Calendar.MINUTE,min);
        return calendar.getTime();
    }


    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int min=calendar.get(Calendar.MINUTE);
        //获取20分钟的时间
        //   min +=20;

        //获取20分钟前的时间
        min -=20;
        calendar.set(Calendar.MINUTE,min);
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(simpleDateFormat.format(date));




    }
}
